package com.mr.cai.main.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;

import com.cai.mr.lib.utils.NetWorkTypeUtils;
import com.mr.cai.main.MrCaiApplication;
import com.mr.cai.main.R;
import com.mr.cai.main.activity.base.MrCaiBaseActivity;
import com.mr.cai.main.adapter.TicketsAdapter;
import com.mr.cai.main.bean.TicketsInfoBean;
import com.mr.cai.main.bean.TicketsInfoBeanList;
import com.mr.cai.main.httpApi.MrCaiHttpApi;
import com.mr.cai.main.parse.TicketsInfoBeanListParse;
import com.mr.cai.main.utils.CommonCoreUtil;
import com.mr.cai.main.view.PublicLoadLayout;
import com.mr.cai.main.view.xlistview.XListView;

import java.util.ArrayList;

import rx.Observable;
import rx.Subscriber;
import rx.Subscription;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action0;
import rx.functions.Action1;

/**
 * Created by hai8108 on 16/2/25.
 */
public class MrCaiTicketsActivity extends MrCaiBaseActivity {
    private XListView xListView;
    private TicketsAdapter adapter;
    private PublicLoadLayout mPublicLayout;
    private ArrayList<TicketsInfoBean> ticketsInfoBeans;
    private Subscription ticketsSubscription;
    public static void launch(Activity activity){
        Intent intent = new Intent(activity, MrCaiTicketsActivity.class);
        activity.startActivity(intent);
    }
    @Override
    public void onCreate (Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mPublicLayout = CommonCoreUtil.createPage(this, R.layout
                .activity_tickets_center_layout);
        setContentView(mPublicLayout);
        mPublicLayout.setmRefreshData(new PublicLoadLayout.RefreshData() {
            @Override
            public void refreshData () {
                requestCartData();
            }
        });
        findView();
        initData();
    }
    private void findView(){
        CommonCoreUtil.setClickAction(findViewById(R.id.top_view).findViewById(R.id.pub_top_left)
                , new Action1() {
            @Override
            public void call (Object o) {
                finish();
            }
        });
        ((TextView)findViewById(R.id.top_view).findViewById(R.id.pub_top_mid)).setText(R.string
                .my_sum_ticket_tv);
        xListView = (XListView) mPublicLayout.findViewById(R.id.tickets_xlist);
        xListView.setScrollable(false);
        xListView.setPullLoadEnable(false);
        xListView.setXListViewListener(ixListViewListener);
        adapter = new TicketsAdapter(this);
        xListView.setAdapter(adapter);
    }
    private XListView.IXListViewListener ixListViewListener = new XListView.IXListViewListener() {
        @Override
        public void onRefresh(XListView view) {
            if (!NetWorkTypeUtils.isNetAvailable(MrCaiApplication.getInstance())) {
                requestCartData();
            }else{
                xListView.stopRefresh();
                CommonCoreUtil.showToast(R.string.public_loading_net_null_err_txt);
            }
        }
        @Override
        public void onLoadMore(XListView view) {

        }
    };
    private void initData(){
        requestCartData();
    }

    private void requestCartData(){
                Observable<TicketsInfoBeanList> ticketsObservable = MrCaiHttpApi
                        .requestTicketsList(MrCaiApplication.getInstance().getCustomerId(), new
                                TicketsInfoBeanListParse());
                ticketsSubscription = ticketsObservable.observeOn(AndroidSchedulers.mainThread())
                .doOnSubscribe(new Action0() {
                    @Override
                    public void call () {
                        mPublicLayout.loading(true);
                    }
                })
                .subscribe(new Subscriber<TicketsInfoBeanList>() {
                    @Override
                    public void onCompleted () {
                        mPublicLayout.finish();
                    }
                    @Override
                    public void onError (Throwable e) {
                        mPublicLayout.dataError(true);
                    }
                    @Override
                    public void onNext (TicketsInfoBeanList mTicketsInfoBeanList) {
                        xListView.stopLoadMore();
                        xListView.stopRefresh();
                        if(mTicketsInfoBeanList.isResult() && mTicketsInfoBeanList.getCode() == 1){
                            adapter.setList(mTicketsInfoBeanList.getData());
                        } else {
                            mPublicLayout.dataNull(true);
                        }
                    }
                });
    }
}
